<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="policyFormRules" :model="policyForm" label-position="left" label-width="140px">
      <el-form-item label="发文机关" prop="issuingAuthority">
        <el-input v-model="policyForm.issuingAuthority" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="policyForm.title" />
      </el-form-item>
      <el-form-item label="索引" prop="indexCode">
        <el-input v-model="policyForm.indexCode" />
      </el-form-item>
      <el-form-item label="索引" prop="issuedCode">
        <el-input v-model="policyForm.issuedCode" />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input v-model="policyForm.category" />
      </el-form-item>
      <el-form-item label="主题词" prop="subject">
        <el-input v-model="policyForm.subject" />
      </el-form-item>
      <el-form-item label="成文日期" prop="writtenDate">
        <el-input v-model="policyForm.writtenDate" />
      </el-form-item>
      <el-form-item label="发文日期" prop="releaseDate">
        <el-input v-model="policyForm.releaseDate" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
          <Tinymce ref="editor" v-model="policyForm.content" :height="400" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="policyForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="policyForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="policyForm.status.value === 5 && policyForm.ID > 0" type="danger" @click="handleDelete(policyForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { savePolicy, removePolicyByID, getPolicyDetail } from '@/api/knowledge/policy'
import { getIndustrialUsable } from '@/api/knowledge/industrial'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Tinymce},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      policyForm: {
        code: '',
        title: '',
        policyID:0,
        employeeID:0,
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      industrialOptions:[],
      policyFormRules: []
    }
  },
  created(options) {
    let policyID = this.$route.query && this.$route.query.policyID
    if (policyID !== undefined) {
      policyID = parseInt(policyID)
      if (!isNaN(policyID)) {
        this.listLoading = true
        const formData = {
          'policyID': policyID
        }
        getPolicyDetail(formData).then(response => {
          if (response.status === 200) {
            this.policyForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(policyID)
  },
  methods: {
    loadEmployeeList() {
      getIndustrialUsable().then(response => {
        if (response.status === 200) {
          this.industrialOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'policy' })
    },
    draftData() {
      this.listLoading = true
      const _policyForm = this.policyForm
      _policyForm.status = 1
      if (_policyForm.ID > 0) {
        _policyForm.policyID = _policyForm.ID
      }
      savePolicy(_policyForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'policy' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _policyForm = this.policyForm
      _policyForm.status = 6
      if (_policyForm.ID > 0) {
        _policyForm.policyID = _policyForm.ID
      }
      savePolicy(_policyForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'policy' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'policyID': row.ID
      }
      removePolicyByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'policy' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
